package com.asiainfo.hybridapp.JsCallAndroid;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.asiainfo.hybridapp.configs.ConstantsME;
import com.asiainfo.hybridapp.configs.InterfaceConfig;
import com.asiainfo.hybridapp.configs.RealInterfaceConfig;
import com.framework.utils.KeyBoardUtil;
import com.framework.utils.PreferencesHelper;
import com.framework.utils.ToastUtil;
import com.framework.utils.Y;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * js调用android
 *
 * @author YobertJomi
 * className JsCallAndroid
 * created at  2017/7/14  9:43
 * android调用js可以直接调用网页中js，也可以注入js代码（字符串）
 * 调用js代码参数里面含字符串用单引号''包裹
 * webView.loadUrl("javascript:jsMethod('" + "我是字符串" + "')");
 */
public class JsCallAndroid {
    private Context context;
    private WebView webView;

    public JsCallAndroid(Context context, WebView webView) {
        this.context = context;
        this.webView = webView;
    }

    @JavascriptInterface
    public void clickOnAndroid() {
        ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
//                ToastUtil.getInstance().showToast("js 调用android--");
                webView.loadUrl("javascript:displayDate()");
            }
        });
    }

    @JavascriptInterface
    public void getLatLngOnAndroid() {
        ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
//                ToastUtil.getInstance().showToast("js 调用android--");
//                webView.loadUrl("javascript:getLatLng(110,115)");
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("province", "重庆市");
                    jsonObject.put("city", "重庆市");
                    jsonObject.put("district", "渝北区");
                    jsonObject.put("address", "重庆市渝北区黄杨路附53号靠近麒麟座c");
                    jsonObject.put("longitude", "106.498838");
                    jsonObject.put("latitude", "29.61429");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Y.y("jsonObject:" + jsonObject.toString());
                webView.loadUrl("javascript:getLatLng('" + String.valueOf("nullfff") + "')");
            }
        });
    }

    @JavascriptInterface
    public void getLatLng() {
        ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtil.getInstance().showToast("js 调用android--clickOnAndroidGetLatLng");
                webView.loadUrl("javascript:getLatLng('" + PreferencesHelper.getInstance().getStringData(ConstantsME.LATITUDE) + "','" + PreferencesHelper.getInstance().getStringData(ConstantsME.LONGITUDE) + "')");
            }
        });
    }

    @JavascriptInterface
    public void nativeLogin() {
        ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webView.loadUrl("javascript:dispalyDate()");
            }
        });
    }

    @JavascriptInterface
    public void nativeGoBack() {
        ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                goBack();
            }
        });
    }

    private void goBack() {
        if (null != webView && webView.canGoBack() && !TextUtils.equals(webView.getUrl(),
                RealInterfaceConfig.getRealBaseServerUrl() + InterfaceConfig.webMain)) {
            webView.goBack();
        } else {
            KeyBoardUtil.getInstance().isCloseSoftInputMethod(context, null, true);
            ((Activity) context).finish();
        }
    }
}
